package com.example.scollpicker;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private String hourString = "0";
    private String minString = "0";
    private int leftTime;//剩余秒数
    private final List<DataBean> hourBeanList = new ArrayList<>();
    private final List<DataBean> minBeanList = new ArrayList<>();
    private TextView text;
    private View picker;




    final Handler handlerStop = new Handler(Looper.myLooper()) {
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                leftTime = 0;
                handler.removeCallbacks(update_thread);
            }
            super.handleMessage(msg);
        }
    };


    Handler handler = new Handler(Looper.myLooper());

    Runnable update_thread = new Runnable() {
        @Override
        public void run() {
            leftTime--;
            if (leftTime > 0) {
                //倒计时效果展示
                String formatLongToTimeStr = formatLongToTimeStr(leftTime);
                text.setText(formatLongToTimeStr);
                //每一秒执行一次
                handler.postDelayed(this, 1000);
            } else {
                //倒计时结束,处理逻辑
                leftTime = 0;
                text.setVisibility(View.GONE);
                picker.setVisibility(View.VISIBLE);

                /**
                 * 倒计时结束的弹窗
                 */



                //发送消息，结束倒计时
                Message message = new Message();
                message.what = 1;
                handlerStop.sendMessage(message);
            }
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initDataList();


        PickerScrollView hourPicker = findViewById(R.id.hour_picker);
        PickerScrollView minPicker = findViewById(R.id.min_picker);
        Button btn = findViewById(R.id.btn);
        text = findViewById(R.id.text);
        picker = findViewById(R.id.picker);

        //设置数据，默认选择第一条
        hourPicker.setData(hourBeanList);
        hourPicker.setSelected(0);
        //滚动监听
        hourPicker.setOnSelectListener(pickers -> hourString = pickers.getCategoryName());


        //设置数据，默认选择第一条
        minPicker.setData(minBeanList);
        minPicker.setSelected(0);
        //滚动监听
        minPicker.setOnSelectListener(pickers -> minString = pickers.getCategoryName());


        //确认按钮
        btn.setOnClickListener(v -> {
            int hour = Integer.parseInt(hourString);
//            int min = Integer.parseInt(minString);
            float min = Float.parseFloat(minString);
            if(hour == 0 && min == 0){
                Toast.makeText(MainActivity.this,"尚未选择时间",Toast.LENGTH_SHORT).show();
            }else {
//                leftTime = (hour * 60 + min) * 60;
                leftTime = (int)((hour * 60 + min) * 60);
                handler.postDelayed(update_thread, 1000);

                String s = formatLongToTimeStr(leftTime);
                text.setText(s);
                picker.setVisibility(View.GONE);
                text.setVisibility(View.VISIBLE);
            }
        });

    }



    private void initDataList() {
        DataBean hourBean0 = new DataBean("0","0","0");
        DataBean hourBean1 = new DataBean("1","1","0");
        DataBean hourBean2 = new DataBean("2","2","0");
        DataBean hourBean3 = new DataBean("3","3","0");
        DataBean hourBean4 = new DataBean("4","4","0");
        hourBeanList.add(hourBean0);
        hourBeanList.add(hourBean1);
        hourBeanList.add(hourBean2);
        hourBeanList.add(hourBean3);
        hourBeanList.add(hourBean4);

        DataBean minBean0 = new DataBean("0","10","0");
        DataBean minBean1 = new DataBean("1","20","0");
        DataBean minBean2 = new DataBean("2","30","0");
        DataBean minBean3 = new DataBean("3","40","0");
        DataBean minBean4 = new DataBean("4","50","0");
        DataBean minBean5 = new DataBean("4","0.1","0");
        DataBean minBean6 = new DataBean("4","0.2","0");
        minBeanList.add(minBean0);
        minBeanList.add(minBean1);
        minBeanList.add(minBean2);
        minBeanList.add(minBean3);
        minBeanList.add(minBean4);
        minBeanList.add(minBean5);
        minBeanList.add(minBean6);
    }




    private String formatLongToTimeStr(int s) {
        int hour = 0;
        int minute = 0;
        int second = s;

        if (second > 60) {
            minute = second / 60;   //取整
            second = second % 60;   //取余
        }
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        return "剩余："+ hour +":"+minute+":"+second;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        leftTime = 0;
        handler.removeCallbacks(update_thread);
    }
}